import req from "@/http"
import { ResquestPaginationType } from './ResquestPaginationType'
import { ResponseBase } from './ResponseBase'
export interface LoginDto {
  qq: string
  password: string
}


export const login = async (data: LoginDto) => {
  const { res, err, isSuccess } = await req<string>({
    method: "post",
    url: "/login",
    data,
  })
  return isSuccess ? res : err
}

export interface registerDto {
  nickName: string
  qq: string
  password: string
}




export const register = async (data: registerDto) => {
  const { res, err, isSuccess } = await req<null>({
    method: "post",
    url: "/login/register",
    data,
  })
  return isSuccess ? res : err
}

export interface CreatePostDto {
  postTitle: string
  postCategoryId: number
  postCover: string
  postContent: string
  postStatus: string
  postCoverConfig: string
}


export const addPost = async (data: CreatePostDto) => {
  const { res, err, isSuccess } = await req<null>({
    method: "post",
    url: "/post/add",
    data,
  })
  return isSuccess ? res : err
}


export interface UpdatePostDto extends CreatePostDto {
  id: number
}

export const updatePost = async (data: UpdatePostDto) => {
  const { res, err, isSuccess } = await req<null>({
    method: "put",
    url: `/post/update`,
    data,
  })
  return isSuccess ? res : err
}

export const upload = async (data: FormData, config) => {
  const { res, err, isSuccess } = await req<{
    destination: string,
    encoding: string,
    fieldname: string
    filename: string,
    mimetype: string,
    originalname: string,
    path: string,
    size: number
    url: string
  }>({
    method: "post",
    url: "/upload",
    data,
    headers: {
      'Content-Type': 'multipart/form-data'
    },
    ...config
  })
  return isSuccess ? res : err
}
export interface Category {
  category_id: number,
  category_name: string
}

export const getAllCategroy = async () => {
  const { res, err, isSuccess } = await req<Array<Category>>({
    method: "get",
    url: "/category/list",
  })
  return isSuccess ? res : err
}


export interface Post {
  id: number
  postTitle: string
  postCategoryId: number
  postCover: string
  postContent: string
  postStatus: '1' | '2' | '3'
  postCoverConfig: string
  likes: number
  createTime: string
  updateTime: string
  createBy: string
  categoryName?: string
  avatar: string
  nickName: string
  userId: number
  category: {
    id: number
    name: string
  }
  userAvatar?: string
  userName?: string
}
export type PostRes = ResponseBase<Array<Post>> & ResquestPaginationType
export interface PostQuery {
  pageNum: number
  pageSize: number
  postStatus: string
  categoryId?: number
}

//获取推荐数据
export const getPostList = async (query: PostQuery): Promise<PostRes> => {
  const { res, err, isSuccess } = await req<Array<Post>>({
    method: "get",
    url: "/post/recommended",
    params: query
  })
  return isSuccess ? res as PostRes : err as PostRes
}


//获取分类下数据
export const getPostListByCategory = async (query: PostQuery): Promise<PostRes> => {
  const { res, err, isSuccess } = await req<Array<Post>>({
    method: "get",
    url: "/post/list",
    params: query
  })
  return isSuccess ? res as PostRes : err as PostRes
}


//输入内容查询文章
export const getPostByContent = async (content: string) => {
  const { res, err, isSuccess } = await req<Array<Post>>({
    method: "get",
    url: "/post/getPostByContent",
    params: {
      content,
    }
  })
  return isSuccess ? res : err
}


//根据 id查询文章详情
export const getPostInfoById = async (id: number) => {
  const { res, err, isSuccess } = await req<Post>({
    method: "get",
    url: `/post/info/${id}`,

  })
  return isSuccess ? res : err
}


export interface User {
  id: number
  qq: string
  password: string
  avatar: string
  status: 0 | 1
  nickName: string
  sex: number
  phoneNumber: string
  email: string
  blogUrl: string
  intro: string
  createTime: string
}


//获取用户信息
export const getUserInfo = async () => {
  const { res, err, isSuccess } = await req<User>({
    method: "get",
    url: `/user/getUserInfo`,

  })
  return isSuccess ? res : err
}


export interface UpdateUserDto {
  avatar: string
  nickName: string
  intro: string
}
//修改用户信息
export const updateUserInfo = async (data: UpdateUserDto) => {
  const { res, err, isSuccess } = await req<User>({
    method: "put",
    url: `/user/updateUser`,
    data,

  })
  return isSuccess ? res : err
}



//获取用户文章
export const getPostByUser = async (postStatus: string) => {
  const { res, err, isSuccess } = await req<Array<Post>>({
    method: "get",
    url: "/post/getPostByUser",
    params: {
      postStatus,
    }
  })
  return isSuccess ? res : err
}


//获取用户文章
export const postDel = async (id: number) => {
  const { res, err, isSuccess } = await req<null>({
    method: "delete",
    url: `/post/del/${id}`,
  })
  return isSuccess ? res : err
}



export interface Comment {
  id: number
  postId: string
  content: string
  replyId: string
  ip: string
  country: string
  address: string
  browser: string
  browserKernel: string
  browserVersion: string
  system: string
  systemVersion: string
  operator: string
  createTime: string
  createBy: string
  children: Array<Omit<Comment, 'children'>>
  avatar: string
  nickName: string
  blogUrl: string
  email: string
  qq: string
  sex: number
  isOpenReply: boolean
  isFirst: boolean
  replyUserId?: string
  replyUser?: {
    avatar: string,
    blogUrl: string,
    nickName: string,
    qq: string,
    sex: number
  }
}


//获取文章下评论
export const getCommentByPostId = async (postId: number) => {
  type res = {
    record: Array<Comment>
    total: number
  }
  const { res, err, isSuccess } = await req<res>({
    method: "get",
    url: `/comment/getCommentByPostId`,
    params: {
      postId,
    }
  })
  return isSuccess ? res : err
}


export interface CreateCommentDto {
  postId: number
  content: string
}

//发布评论
export const addComment = async (data: CreateCommentDto) => {
  const { res, err, isSuccess } = await req<null>({
    method: "post",
    url: `/comment/add`,
    data,
  })
  return isSuccess ? res : err
}

export interface ReplyCommentDto extends CreateCommentDto {
  replyId: number
  replyUserId: string
}

//回复评论
export const replyComment = async (data: ReplyCommentDto) => {
  const { res, err, isSuccess } = await req<null>({
    method: "post",
    url: `/comment/reply`,
    data,
  })
  return isSuccess ? res : err
}



